/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.src.uniandes.ecos.tsp.creditscore.algorithm;

import com.src.uniandes.ecos.tsp.creditscore.entity.ClienteProducto;
import com.src.uniandes.ecos.tsp.creditscore.enums.CreditCardTypePointsEnum;
import com.src.uniandes.ecos.tsp.creditscore.enums.InquiriesPointsEnum;
import com.src.uniandes.ecos.tsp.creditscore.exception.CreditScoreException;
import com.src.uniandes.ecos.tsp.creditscore.helper.CreditScoreHelper;
import java.util.List;

/**
 *
 * @author djoya
 */
public class RulesNewCredit {

    /**
     * constante 0.
     */
    private static final double ZERO = 0;
    /**
     * @param clientCode.
     * @return puntos de indagacion.
     * @throws CreditScoreException.
     */
    public final double getPointsCreditInquiries(final Integer clientCode)
            throws CreditScoreException {

        double points = InquiriesPointsEnum.HIGH.getValue();

        try {
            final long creditInquiries = CreditScoreHelper.getInstance()
                    .getLastMonthsCreditInquiries(clientCode,
                    Integer.valueOf(6));

            if(creditInquiries >= 1 && creditInquiries <= 3){
                points = InquiriesPointsEnum.MEDIUM.getValue();
            }else if(creditInquiries > 3){
                points = InquiriesPointsEnum.LOW.getValue();
            }
        } catch (CreditScoreException e) {
            throw new CreditScoreException(
                    "RulesNewCredit:getPointsCreditInquiries:"
                    + e.toString(), e);
        }
       return points;
    }

    public double getPointsCreditCardProducts(final Integer clientCode)
            throws CreditScoreException {

        double points = 0;

        try {
            final List<ClienteProducto> products = CreditScoreHelper.
                    getInstance().getCreditCardProducts(clientCode);

            for (ClienteProducto clienteProducto : products) {

                if(clienteProducto.getProducto().getProCodigo().intValue() == 1
                        || clienteProducto.getProducto()
                        .getProCodigo().intValue() == 2
                        || clienteProducto.getProducto()
                        .getProCodigo().intValue() == 3){
                    points += CreditCardTypePointsEnum.HIGH.getValue();
                }else if(clienteProducto.getProducto()
                        .getProCodigo().intValue() == 7) {
                    points += CreditCardTypePointsEnum.LOW.getValue();
                }
            }

            if(points != ZERO) {
                points = points / products.size();
            } else {
                points = ZERO;
            }
            
        } catch (CreditScoreException e) {
            throw new CreditScoreException(
                    "RulesNewCredit:getPointsCreditCardProducts:"
                    + e.toString(), e);
        }
        return points;
    }
}
